package com.eascs.p2p.admin.manager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.admin.manager.IAdminRoleManager;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.AdminRoleEntity;
import com.eascs.p2p.common.entity.AdminRoleResourceEntity;
import com.eascs.p2p.common.vo.UserRoleVo;



@Repository
public class AdminRoleManagerImpl extends BaseDao implements IAdminRoleManager {

	@Override
	public PageResult<Object[]> selectRolesPage(UserRoleVo role) {
	
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, role.getPageSize(), role.getShowPage());
		
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT role.id,role.role_code,role.role_name,role.description FROM  p2p_admin_role role");
		sql.append(" WHERE 1=1");
		if (StringUtil.isNotBlank(role.getRoleName())) {
			sql.append(" and role.role_name like ? ");
			sqlPageQuery.addParam("%"+role.getRoleName()+"%");
		}
		if (StringUtil.isNotBlank(role.getRoleCode())) {
			sql.append(" and role.role_code like ? ");
			sqlPageQuery.addParam("%"+role.getRoleCode()+"%");
		}
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;
	}


	@Override
	public String insertRole(AdminRoleEntity role) {
		Date date = new Date();
		role.setCreateTime(date);
		role.setUpdateTime(date);

		boolean flag = this.insert(role);
		if (flag == true) {
			return role.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean deleteRole(String roleId) {
		StringBuffer sql=new StringBuffer();
    	sql.append("DELETE FROM p2p_admin_role WHERE id =?");
    	return this.execute(sql.toString(), new Object[]{roleId});
	}
	
	@Override
	public boolean isExistRoleRes(String roleId) {
		boolean flag=false;
		StringBuffer sql=new StringBuffer();
    	sql.append("select * FROM p2p_admin_role_resources WHERE role_id =?");
    	List<Object[]> list = this.findObjectsList(sql.toString(), roleId);
    	if(list!=null&&list.size()>0){
    		flag=true;
    	}
    	return flag;
	}
	
	@Override
	public boolean deleteRoleRes(String roleId) {
		StringBuffer sql=new StringBuffer();
    	sql.append("DELETE FROM p2p_admin_role_resources WHERE role_id =?");
    	return this.execute(sql.toString(), new Object[]{roleId});
	}
	
	
	@Override
	public boolean isExistRoleUser(String roleId) {
		boolean flag=false;
		StringBuffer sql=new StringBuffer();
    	sql.append("select * FROM p2p_admin_user_role WHERE role_id =?");
    	List<Object[]> list = this.findObjectsList(sql.toString(), roleId);
    	if(list!=null&&list.size()>0){
    		flag=true;
    	}
    	return flag;
	}
	@Override
	public boolean deleteRoleUser(String roleId) {
		StringBuffer sql=new StringBuffer();
    	sql.append("DELETE FROM p2p_admin_user_role WHERE role_id =?");
    	return this.execute(sql.toString(), new Object[]{roleId});
	}
	
	
	@Override
	public boolean insertRoleResBatch(List<AdminRoleResourceEntity> roleResList) {
		boolean flag=true;
		for (AdminRoleResourceEntity r : roleResList) {
			flag = this.insert(r);
			if(flag==false){
				break;
			}
		}
		return flag;
	}


	@Override
	public AdminRoleEntity getAdminRoleById(String roleId) {
		return this.getById(AdminRoleEntity.class, roleId);
	}

	
	
	
	
	@Override
	public List<Object[]> getRoleName(String keywords) {

		List<Object[]> dataSouceList = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,role_name,role_code from p2p_admin_role t  where 1=1");
		sql.append(" and role_code like ?");
		sql.append(" or role_name  like ?");
		sql.append(" order by id ");
		sql.append(" limit 0,10 ");
		dataSouceList = this.findObjectsList(sql.toString(), "%" + keywords + "%", "%" + keywords + "%");
		return dataSouceList;
	}

	

	
	
	
	
	@Override
	public boolean updateRole(AdminRoleEntity role) {
		Date date = new Date();
		role.setUpdateTime(date);

		return this.updateById(role);
	}


	@Override
	public List<AdminRoleEntity> selectRolesByUserNo(String userNo) {
		StringBuffer sql=new StringBuffer();
        sql.append(" SELECT role.id,role.role_name,role.role_level,role.description,role.creator,role.updator,role.create_time,role.update_time");
        sql.append(" FROM  p2p_admin_user_role sur ");
        sql.append(" LEFT JOIN p2p_admin_role role ON role.id = sur.role_id");
        sql.append(" WHERE sur.admin_id = ?");
       
        return this.findEntitys(sql.toString(), AdminRoleEntity.class, userNo);
	}





}
